/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 字符串.练习;

import org.junit.Test;

public class StringDemo {


    /**
     * @description 返回最大子字符串
     *              前提只有一对相同最大子字符串
     * @param str1 一个字符串
     * @param str2 另一个字符串
     * @return [java.lang.String, java.lang.String]
     * @author 俊昭
     * @date 2022/4/16
    */
    public static String MaxLengthSameString(String str1, String str2){
        if (str1==null|str2==null){
            return null;//判空
        }
        int x = str1.length();
        int y = str2.length();
        String longer = (x>=y)?str1:str2;
        String shorter = (x<y)?str1:str2;
        int len = shorter.length();


        for (int i = 0; i < len ; i++) {
            for (int a = 0,b = len - i;b!=len;a++,b++) {
                String sub =shorter.substring(a,b);
                if (longer.contains(sub)){
                    return sub;
                }
            }
        }
        return null;
    }
    @Test
    public void MaxLengthSameStringTest(){
        System.out.println(MaxLengthSameString("123456", "223455"));

    }
    /**
     * @description 获取子字符串在主字符串中出现的次数
     * @param str 主字符串
     * @param s 子字符串
     * @author 俊昭
     * @date 2022/4/16
     */
    public static int appearTimes02(String str, String s){
        if (str==null|s==null){
            return -1;
        }
        int times = -1;

        int index = 0;
        int main = str.length();
        int sub = s.length();

        if (main>=sub){
//            while ((index=str.indexOf(s)) != -1){
////                index=str.indexOf(s);这里在while的条件判断中已经给出了赋值
//                times++;
//                // 截取
//                str = str.substring(index + sub);
//            }

            while((index=str.indexOf(s,index)) != -1){
//                这里在while的条件判断中已经给出了赋值
                times++;
                // 截取
                index+=sub;
            }

            return times+1;

        }

        return times;

    }

    /**
     * @description 获取子字符串在主字符串中出现的次数
     * @param str 主字符串
     * @param s 子字符串
     * @author 俊昭
     * @date 2022/4/16
    */
    public static int appearTimes01(String str, String s){
        if (str==null|s==null){
            return -1;
        }
        int times = -1;

        int main = str.length();
        int sub = s.length();
        if (main>=sub){
            String s1 = str.replace(s,"");
            times = (main - s1.length())/sub;
        }

        return times;

    }

    /**
     * @description 反转字符串中的一部分
     *     转换为数组
     *
     * @author 俊昭
     * @date 2022/4/16
     */
    public static String reverse(String str,int startIndex,int endIndex){
        if (str != null && str.length()!=0) {
            char[] arr = str.toCharArray();
            char temp;

            for (int i = startIndex, j = endIndex;i<j;i++,j--){
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
            return new String(arr);

        }
        return new NullPointerException().toString();
    }

    @Test
    public void testReverse(){
//        System.out.println(reverse("123456", 2, 5));
//        System.out.println(appearTimes01("111222333444", "2"));
        System.out.println(appearTimes02("111222333444", "2"));
    }
}
